package com.xckj.teacher.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.UnitBean;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 选择单元
 * @Date: 2024/6/13 10:56
 */
public class SelectedUnitAdapter extends RecyclerView.Adapter {
    private List<UnitBean> unitBeans;
    private OnItemClickListener onItemClickListener;


    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<UnitBean> unitBeans) {
        this.unitBeans = unitBeans;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unit, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        UnitBean unitBean = unitBeans.get(position);
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(unitBean.unitid, unitBean.versionId);
            }
        });
        itemViewHolder.tvUnit.setText(String.format("· %s", unitBean.name));
    }

    @Override
    public int getItemCount() {
        return unitBeans == null ? 0 : unitBeans.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvUnit;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUnit = itemView.findViewById(R.id.tv_unit);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param unitId 单元Id
         * @param versionId 版本Id
         */
        void onItemClick(String unitId,String versionId);
    }
}
